package main;

import java.io.IOException;

import communication.KanInitieleVerbindingNietMaken;
import communication.KanaalUiteinde;
import communication.PacmanProtocol;
import communication.Protocol;
import communication.RabbitMQKanaalUiteinde;

import util.Coordinaat;

import gui.GUI;
import information.Kaart;
import information.Pacman;
import mapReader.KaartLezer;

public class Main {
	
	
	
	public void init(String fileName, String host, String exchange)
	{		
		KaartLezer kaartlezer = new KaartLezer();
		
		Kaart map;
		
		try
		{
			map = kaartlezer.lees(fileName);
		}	
		catch (IOException e) {
			System.err.println("Could not read map");
			return;
		}
		
		
		
		Coordinaat positionPacman = map.getLaatsteLocatiePacMan();
		
		if(positionPacman == null)
		{
			System.err.println("Map has no pacman");
			return;
		}
		
		Pacman pacman = new Pacman(positionPacman);
		
		KanaalUiteinde rabbitMQ;
		
		try
		{
			rabbitMQ = new RabbitMQKanaalUiteinde(host, exchange);
		}
		catch(KanInitieleVerbindingNietMaken e)
		{
			System.err.println("Could not connect to the RabbitMQ-server");
			return;
		}
		
		PacmanProtocol pacmanProtocol = new PacmanProtocol(pacman);
		
		rabbitMQ.setProtocol(pacmanProtocol);
		pacmanProtocol.setKanaal(rabbitMQ);
		pacmanProtocol.pacmanMoved(pacman.getPosition());
		
		new GUI(pacman, map);
	}
	
	public static void main(String[] args) {
		
		if(args.length != 3)
		{
			System.err.println("Usage: java -jar pacman.jar <fileName Map> <host rabbitMQ> <name exchange>");
			return;
		}
		
		new Main().init(args[0], args[1], args[2]);
	}
}
